package com.example.simplecardtenantbank.controller;

import com.example.simplecardtenantbank.DTO.CreditListDTO;
import com.example.simplecardtenantbank.DTO.KaBaoUserDTO2;
import com.example.simplecardtenantbank.Utils.ResultVOUtil;
import com.example.simplecardtenantbank.VO.ResultVO;
import com.example.simplecardtenantbank.VO.User3VO;
import com.example.simplecardtenantbank.VO.UserJson2DataVO;
import com.example.simplecardtenantbank.client.KaBaoUserClient;
import com.example.simplecardtenantbank.service.CreditCardService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/card")
public class CreditCardController {

    @Autowired
    private CreditCardService creditCardService;

    @SuppressWarnings("all")
    @Autowired
    private KaBaoUserClient kaBaoUserClient;

    @GetMapping(value = "/list")
    public ResultVO list(HttpServletRequest request) {
        String token = request.getHeader("Access-User-Token");
        //发送请求获取用户信息(服务间通讯)
        String response = kaBaoUserClient.getInnerUserInfo(token);
        Gson gson = new Gson();
        User3VO user3VO = gson.fromJson(response,User3VO.class);
        UserJson2DataVO userJson2DataVO = user3VO.getData();
        System.out.println(userJson2DataVO.getUserinfo().toString());
        KaBaoUserDTO2 kaBaoUserDTO2 = userJson2DataVO.getUserinfo();
        String userId = String.valueOf(kaBaoUserDTO2.getUid());
        List<CreditListDTO> tCardList = creditCardService.findByUserId(userId);
        return ResultVOUtil.success(tCardList);
    }
}
